package eva.savant.business.ventas.view

import eva.savant.businessobject.gl.Persona
import eva.savant.businessobject.vt.Canal
import eva.savant.businessobject.vt.Cliente

import javax.faces.bean.ManagedBean
import javax.faces.bean.SessionScoped
import javax.annotation.PostConstruct
import javax.faces.convert.Converter

@SessionScoped
@ManagedBean(name = 'IMCliente')
public class IMCliente extends Maestro {

  def lsCanal, dias

  @PostConstruct
  public void cleanEntity() {

    entity= new Cliente(); init()

    entity.fecha= new Date()

    entity.venta= new Date()

    entity.canal= new Canal()

    entity.persona= new Persona()

    lsCanal= find(Canal.class,'')

    dias= [false,false,false,false,false,false,false]
  }

  public boolean mergeEntity(Object entity) { entity= (Cliente) entity

    entity.visita= ''
    if(dias[0]== true) entity.visita= entity.visita + 'L'
    if(dias[1]== true) entity.visita= entity.visita + 'M'
    if(dias[2]== true) entity.visita= entity.visita + 'X'
    if(dias[3]== true) entity.visita= entity.visita + 'J'
    if(dias[4]== true) entity.visita= entity.visita + 'V'
    if(dias[5]== true) entity.visita= entity.visita + 'S'
    if(dias[6]== true) entity.visita= entity.visita + 'D'

    return merge(entity) != null
  }

  public void editEntity(Object id) {

    entity= get(Cliente, id)

    dias= [false,false,false,false,false,false,false]

    for(int i= 0; i<entity.visita?.length(); i++) {
    def x= entity.visita.charAt(i)
    if (x=='L') dias[0]= true
    if (x=='M') dias[1]= true
    if (x=='X') dias[2]= true
    if (x=='J') dias[3]= true
    if (x=='V') dias[4]= true
    if (x=='S') dias[5]= true
    if (x=='D') dias[6]= true }
  }

  public List complete(String value) { return completeLike(Persona.class,'nombre',value) }

  public Converter getConverter() { return converterLike(Persona.class) }

}
